We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

application->handle(), stuck (maybe an infinite loop?)

Hi,

I'm using Phalcon as the application framework to use with a Ratchet WebSocket server. Note that Ratchet is a PHP server based on ReactPHP, so it uses the reactor pattern (single thread.)

On a new connection, I just get the uri and call $application->handle($uri). I do the same when I receive a message.

The problem is that when I receive a connection everything works as it is expected, but when I receive a message and I call $application->handle($uri), the server gets stuck. So I think that the problem is caused by the router.

Relevant code:

Phalcon application services (router):

$loader = new Phalcon\Loader();

        $loader->registerDirs(
            array(
                './Mvc/controllers/',
                './Mvc/models/'
            )
        )->register();

        $di = new Phalcon\DI\FactoryDefault();
        $di->set('router', function () {

            $router = new Phalcon\Mvc\Router();
            $router->setDefaultNamespace('Wings\WsServer\Mvc\Controllers');
            $router->add(
                "/raw/echo",
                array(
                    "controller" => "Raw",
                    "action"     => "echo",
                )
            );

            $router->add(
                '/GET/resource/ws/{conn_id:[0-9]+}',
                array(
                    'controller' => 'connection',
                    'action' => 'new'
                )
            );

            $router->add(
                '/GET/resource/thing/query',
                array(
                    'controller' => 'thing',
                    'action' => 'query'
                )
            );

            return $router;

        });

Connection Handler (observe the var_dump($conn->WebSocket->request->getPath()); and var_dump($jsonArray["uri"]);).

   public function onOpen(ConnectionInterface $conn)
    {
        // Store the new connection to send messages to later
        $this->clients->attach($conn);

        echo 'Client connected on WS server!';

        $application = $this->_di->getRaw('application');
        $application->getDI()->set('connection', $conn);

        var_dump($conn->WebSocket->request->getPath());

        $content = $application->handle($conn->WebSocket->request->getPath())->getContent();
        echo 'Client connection handled!';

        $conn->send($content);
    }

    public function onMessage(ConnectionInterface $from, $msg)
    {
        $jsonArray = json_decode($msg, true);
        $application = $this->_di->getRaw('application');
        $application->getDI()->set('connection', $from);
        $application->getDI()->set('data', $jsonArray["query"]);

        var_dump($jsonArray["uri"]);

        $content = $application->handle($jsonArray["uri"])->getContent();
        $from->send($content);
    }

What I get then on the shell is

Client connected on WS server!string(18) "/GET/resource/ws/1"
Client connection handled!string(25) "/GET/resource/thing/query"

and the server gets stuck. If a put an exit(-1); after

$content = $application->handle($jsonArray["uri"])->getContent();

the exit is never reached.

Any help would be appreciated.



696

Well, it seems that the problem was inside the controller:

<?php

namespace Wings\WsServer\Mvc\Controllers;

use Phalcon\Mvc\Controller;

class ThingController extends Controller
{

    public function queryAction()
    {
        $phalconResponse = new \Phalcon\Http\Response;
        $phalconResponse->setContent($this->getDI()->get('data'));

        exit(-1);

        return $phalconResponse;
    }

}

it gets stuck in the line

$phalconResponse->setContent($this->getDI()->get('data'));

It seems that the problem was trying to get an string using a Phalcon\DI:get(), solved using Phalcon\DI:getRaw() instead.